/**
 * <copyright>
 *
 * Copyright (c) 2010 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 * 
 * $Id: Tutorial.java,v 1.4 2010/11/11 10:28:15 mtaal Exp $
 */

package mixer.basededatos;

import java.io.FileOutputStream;
import java.util.Properties;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.eclipse.emf.teneo.hibernate.HbHelper;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.PostgreSQL82Dialect;

import audiomixer.AudiomixerPackage;


/**
 * Quick Start Tutorial
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.4 $
 */
public class TeneoBaseDeDatos {

	/** The main method */
	public static void main(String[] args) {

		// To configure Hibernate, supply properties describing the JDBC driver,
		// URL, username/password and SQL dialect.
		// By default the properties are obtained from the file
		// "hibernate.properties" at the classpath root.
		//
		// Alternatively, you can set the properties programmatically:
		//
		// For more information see <a
		// href="http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html#configuration-programmatic">
		// section 3.1 of the Hibernate manual</a>.
		//
		Properties hibernateProperties = new Properties();
		
		//mysql
//		hibernateProperties.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
//		hibernateProperties.setProperty(Environment.USER, "root");
//		
//		hibernateProperties.setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/mixer");
//		hibernateProperties.setProperty(Environment.PASS, "root");
//		hibernateProperties.setProperty(Environment.DIALECT,
//				MySQLDialect.class.getName());
		
		//postgres
		hibernateProperties.setProperty(Environment.DRIVER, "org.postgresql.Driver");
		hibernateProperties.setProperty(Environment.USER, "postgres");
		hibernateProperties.setProperty(Environment.URL, "jdbc:postgresql://127.0.0.1:5432/mixer");
		hibernateProperties.setProperty(Environment.PASS, "postgres");
		hibernateProperties.setProperty(Environment.DIALECT,PostgreSQL82Dialect.class.getName());

		// set a specific option
		// see this page
		// http://wiki.eclipse.org/Teneo/Hibernate/Configuration_Options
		// for all the available options
		hibernateProperties.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT,
				"REFRESH,PERSIST,MERGE");

		// use the joined inheritance mapping
		hibernateProperties.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "JOINED");
		hibernateProperties.setProperty(PersistenceOptions.IGNORE_EANNOTATIONS, "true");
		hibernateProperties.setProperty(PersistenceOptions.DISABLE_ECONTAINER_MAPPING, "true");
		hibernateProperties.setProperty(PersistenceOptions.ALWAYS_VERSION, "false");
		hibernateProperties.setProperty(PersistenceOptions.DEFAULT_ID_FEATURE_NAME, "id");
		hibernateProperties.setProperty(PersistenceOptions.ID_COLUMN_NAME,"id");
		hibernateProperties.setProperty(PersistenceOptions.JOIN_COLUMN_NAMING_STRATEGY,"simple");
		
		

//		// use an annotations file as an example
//		// this lets the library use a special table
//		hibernateProperties.setProperty(PersistenceOptions.PERSISTENCE_XML,
//				"procesosvg/modelo/annotations.xml");

		// Create the DataStore.
		final String dataStoreName = "MixerDataStore";
		final HbDataStore dataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName);
		dataStore.setDataStoreProperties(hibernateProperties);
		
		// Configure the EPackages used by this DataStore.
		dataStore.setEPackages(new EPackage[] { AudiomixerPackage.eINSTANCE });

		// Initialize the DataStore. This sets up the Hibernate mapping and, in
		// turn, creates the corresponding tables in the database.
		String hibernate_properties = ""; 
		try {
			dataStore.initialize();
		} finally {
			// print the hibernate mapping
			hibernate_properties = dataStore.getMappingXML();
		}

//		final SessionFactory sessionFactory = dataStore.getSessionFactory();
//		{
//			// Open a new Session and start transaction.
//			final Session session = sessionFactory.openSession();
//			session.beginTransaction();
//
//			// Create a library.
//			Usuario usuario = AudiomixerFactory.eINSTANCE.createUsuario();
//			// Make it persistent.
//			session.save(usuario);
//
//			// Commit the changes to the database.
//			session.getTransaction().commit();
//			// Close the session. Not necessary if
//			session.close();
//		}
		
		//write hibernate properties
		try {
			FileOutputStream fos = new FileOutputStream("hibernate.cfg");
			fos.write(hibernate_properties.getBytes());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("error escribiendo archivo");
		}

	}
}
